package com.jason.redCherry.action;

import com.jason.redCherry.common.Global;
import com.jason.redCherry.common.ToolUtil;
import com.jason.redCherry.dao.ConfigMapper;
import com.jason.redCherry.dao.GuestMapper;
import com.jason.redCherry.dao.JSMapper;
import com.jason.redCherry.entity.Config;
import com.jason.redCherry.entity.Guest;
import com.jason.redCherry.entity.JS;
import com.jason.redCherry.entity.User;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by Jason on 2016/9/7.
 */
public class UserAction extends BasicAction {

    private String rule;
    private String jsName;
    private String jsContent;
    private String jsId;
    private String guestName;
    private String guestType;
    private int guestStatus;
    private String guestId;

    @Resource
    private ConfigMapper configMapper;
    @Resource
    private JSMapper jsMapper;
    @Resource
    private GuestMapper guestMapper;

    public String index() {
        return "index";
    }

    public String rule() {
        Config rule = configMapper.selectByKey(Global.CONFIG_RULE);
        setAttr("rule", rule.getSysJson());

        List<JS> jsList = jsMapper.selectAll();
        setAttr("jsList", jsList);

        Config appVersion = configMapper.selectByKey(Global.CONFIG_APP_VERSION);
        setAttr("version", appVersion.getSysVal());

        return "rule";
    }

    public String rule_submit() {
        Config currentRuleConfig = configMapper.selectByKey(Global.CONFIG_RULE);
        currentRuleConfig.setSysJson(rule);
        configMapper.updateByPrimaryKey(currentRuleConfig);
        return "rule_redirect";
    }

    public String update_version() {
        Config currentVersionConfig = configMapper.selectByKey(Global.CONFIG_APP_VERSION);
        currentVersionConfig.setSysVal(ToolUtil.upVersion(currentVersionConfig.getSysVal()));
        configMapper.updateByPrimaryKey(currentVersionConfig);
        return "rule_redirect";
    }

    public String add_js() {
        JS js = new JS();

        if (jsId != null && !"".equals(jsId)) {
            js.setJsId(jsId);
            js.setJsContent(jsContent);
            js.setJsUrl(jsName);
            jsMapper.updateByPrimaryKey(js);
        } else {
            js.setJsId(ToolUtil.getNextValue());
            js.setJsContent(jsContent);
            js.setJsUrl(jsName);
            jsMapper.insert(js);
        }
        return "rule_redirect";
    }

    public String del_js() {
        if (!"".equals(jsId)) {
            jsMapper.deleteByPrimaryKey(jsId);
        }
        return "rule_redirect";
    }


    public String guest_list() {

        User u = (User) getSession(Global.SESSION_CURRENT_USER);

        List<Guest> guestList = guestMapper.selectByoWner(u.getUserName());

        setAttr("guestList", guestList);

        return "guest";
    }


    public String add_guest() {
        User u = (User) getSession(Global.SESSION_CURRENT_USER);
        Guest g = new Guest();
        String[] ids = ToolUtil.getNextValue().split("-");
        g.setGuestId(ids[ids.length - 1]);
        g.setCreateTime(new Date());
        g.setPwd(ids[0]);
        g.setOwner(u.getUserName());
        g.setGuestName(guestName);
        if (guestType.equals("1")) {
            g.setUntilTime(ToolUtil.addDay(new Date(), 365));
        }
        guestMapper.insert(g);
        setSession("guest_last", g);
        return "guest_redirect";
    }


    public String guest_status() {
        User u = (User) getSession(Global.SESSION_CURRENT_USER);
        Guest g = guestMapper.selectByPrimaryKey(guestId);
        if (g != null && g.getOwner().equals(u.getUserName())) {
            g.setStatus(guestStatus);
            guestMapper.updateByPrimaryKey(g);
        }
        return "guest_redirect";
    }


    public String getRule() {
        return rule;
    }

    public void setRule(String rule) {
        this.rule = rule;
    }


    public String getJsName() {
        return jsName;
    }

    public void setJsName(String jsName) {
        this.jsName = jsName;
    }

    public String getJsContent() {
        return jsContent;
    }

    public void setJsContent(String jsContent) {
        this.jsContent = jsContent;
    }

    public String getJsId() {
        return jsId;
    }

    public void setJsId(String jsId) {
        this.jsId = jsId;
    }

    public String getGuestName() {
        return guestName;
    }

    public void setGuestName(String guestName) {
        this.guestName = guestName;
    }

    public String getGuestType() {
        return guestType;
    }

    public void setGuestType(String guestType) {
        this.guestType = guestType;
    }


    public int getGuestStatus() {
        return guestStatus;
    }

    public void setGuestStatus(int guestStatus) {
        this.guestStatus = guestStatus;
    }

    public String getGuestId() {
        return guestId;
    }

    public void setGuestId(String guestId) {
        this.guestId = guestId;
    }
}
